package com.example.springsecurity.service.Impl;

import com.example.springsecurity.hadoop.hive.HbaseClient;
import com.example.springsecurity.service.JsoupService;
import org.jsoup.Jsoup;
import org.springframework.stereotype.Service;

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class JsoupServiceImpl implements JsoupService {
    @Override
    public Integer catchData() throws IOException, ParseException {
        writeHbase();
        return 0;
    }

    public void writeHbase() throws IOException, ParseException {
        String baseUrl="https://bj.zhue.com.cn/search_list.php?sort=&pid=0&s_id=0&cid=0&county_id=0&mid=&lx=&page=";
        String[] fields=new String[] {"data:date","data:province","data:place","data:productName","data:category","data:price","data:unit"};
        List<List<String>> datas=parseUrl(baseUrl);
        if(datas==null||datas.isEmpty()){
            return;
        }
        System.out.println(datas.size());
        for(List<String> data:datas){
            String rowKey = UUID.randomUUID().toString().replace("-","");
            HbaseClient.addRecord("products",rowKey,fields,data);
        }

    }

    public static List<List<String>> parseUrl(String url) throws IOException, ParseException {
        List<List<String>> datas=new ArrayList<>();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();
        String filter=dateFormat.format(new Date(date.getTime() - (long)24 * 60 * 60 * 1000));
        for (int j=1;j<=100;j++){
            System.out.println("page:"+j);
            try {
                Document doc = Jsoup.connect(url+j).timeout(2000).get();
                Elements elements=doc.getElementsByClass("t_f");
                Elements contents  = elements.get(1).getElementsByTag("a");
                for (int i = 5; i < contents.size(); i = i + 8) {
                    List<String> data = new ArrayList<>();
                    data.add(contents.get(i).text());
                    data.add(contents.get(i + 1).text());
                    data.add(contents.get(i + 2).text());
                    data.add(contents.get(i + 3).text());
                    data.add(contents.get(i + 4).text());
//            data.add(contents.get(i+5).text());
                    String[] temp = contents.get(i + 5).text().split(" ");
                    data.add(temp[0]);
                    data.add(temp[1]);
                    if (dateFormat.parse(data.get(0)).after(dateFormat.parse(filter))) {
                        datas.add(data);
                    }
                }
            }catch (Exception e){
            }
        }
        return datas;
    }
}
